﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.quest.Display {
    import flash.events.*;
    import flash.display.*;
    import Classes.util.*;
    import com.greensock.*;
    import Display.*;

    public class Container extends Sprite {

        public static const DIRECTION_VERT:int = 1;
        public static const DIRECTION_HORI:int = 2;
        protected static const DEBUG_MODE:Boolean = false;

        protected const CSLOT_HEIGHT:int = 64;
        protected const SLOT_Y_SPACING:int = 70;

        public var m_containerLock:Boolean;
        public var num_slots:uint = 5;
        public var resourcePool:Array;
        public var m_slotDistance:uint;
        public var m_box_width:uint;
        public var m_box_height:uint;
        public var totalDistance:uint;
        public var constantDistance:uint;
        public var images:Array;
        public var m_i:int = 0;
        public var direction:int;
        protected var visibleWidth:Number;
        protected var visibleHeight:Number;
        protected var m_fadeOutTweenTime:Number = 0.3;
        protected var m_shiftTweenTime:Number = 0.8;
        protected var m_mask:Sprite;

        public function Container(_arg1:uint, _arg2:int, _arg3:int, _arg4:int, _arg5:uint, _arg6:uint, _arg7:int=500, _arg8:int=350):void{
            var _local10:ContainerSlot;
            this.resourcePool = [];
            this.images = [];
            super();
            this.m_box_width = _arg7;
            this.m_box_height = _arg8;
            this.x = _arg3;
            this.y = _arg4;
            this.direction = _arg2;
            this.num_slots = _arg1;
            this.visibleHeight = _arg6;
            this.visibleWidth = _arg5;
            switch (_arg2){
                case DIRECTION_HORI:
                    this.totalDistance = this.visibleWidth;
                    this.constantDistance = this.visibleHeight;
                    break;
                case DIRECTION_VERT:
                    this.totalDistance = this.visibleHeight;
                    this.constantDistance = this.visibleWidth;
                    break;
            };
            this.m_slotDistance = 70;
            this.m_mask = this.generateMask();
            this.addChild(this.m_mask);
            this.mask = this.m_mask;
            var _local9:int;
            while (_local9 < (this.num_slots * 3)) {
                _local10 = new ContainerSlot();
                this.resourcePool.push(_local10);
                addChild(_local10);
                _local9++;
            };
            if (DEBUG_MODE){
                this.debugMode();
            };
        }
        public function getDisplayObjectByName(_arg1:String):DisplayObject{
            var _local2:ContainerSlot;
            for each (_local2 in this.resourcePool) {
                if (!_local2.available){
                    if (_local2.getChildAt(0).name == _arg1){
                        return ((_local2.getChildAt(0) as GameSprite));
                    };
                };
            };
            return (null);
        }
        public function set mask_height(_arg1:Number):void{
            this.m_box_height = _arg1;
            this.m_mask.height = _arg1;
        }
        public function getAvailableResource():ContainerSlot{
            var _local1:ContainerSlot;
            for each (_local1 in this.resourcePool) {
                if (_local1.available){
                    return (_local1);
                };
            };
            return (null);
        }
        public function getNextAvailableSlot():int{
            var _local1:int;
            while (_local1 < this.num_slots) {
                if ((this.resourcePool[_local1] as ContainerSlot).available){
                    return (_local1);
                };
                _local1++;
            };
            return ((this.num_slots - 1));
        }
        public function push(_arg1, _arg2:int=0, _arg3:String="sidebar"):Boolean{
            var _local5:ContainerSlot;
            ButtonManager.buttonize(_arg1, null, EffectsUtil.getGlowEffectByAssignment(_arg3));
            var _local4:ContainerSlot = this.getAvailableResource();
            if (_local4 != null){
                _local4.available = false;
                _local4.addChild(_arg1);
                _local4.position = _arg2;
                _local4.addEventListener(MouseEvent.CLICK, this.slotClick);
                switch (this.direction){
                    case DIRECTION_HORI:
                        _local4.x = (((this.CSLOT_HEIGHT + this.m_slotDistance) / 2) + (_arg2 * this.m_slotDistance));
                        _local4.y = ((-(_local4.height) / 2) + (height / 2));
                        break;
                    case DIRECTION_VERT:
                        _local4.y = ((-((_local4.height + this.m_slotDistance)) / 2) + (_arg2 * this.m_slotDistance));
                        _local4.x = ((-(_local4.width) / 2) + (width / 2));
                        break;
                };
                for each (_local5 in this.resourcePool) {
                    if ((((_local5.available == false)) && ((_local5.position >= _arg2)))){
                        if ((((_arg2 == 0)) || ((((_arg2 > 0)) && ((((_local5 == _local4)) || ((_local5.position > _arg2)))))))){
                            this.shift(_local5);
                        };
                    };
                };
                return (true);
            };
            return (false);
        }
        public function slotClick(_arg1:MouseEvent=null, _arg2:ContainerSlot=null, _arg3:Boolean=true):void{
            var _local4:ContainerSlot;
            this.m_containerLock = true;
            if (_arg1 == null){
                _local4 = _arg2;
            } else {
                _local4 = (_arg1.currentTarget as ContainerSlot);
            };
            var _local5:uint = _local4.position;
            for each (_arg2 in this.resourcePool) {
                if (_arg2.position >= _local5){
                    this.unshift(_arg2);
                };
            };
            _local4.removeEventListener(MouseEvent.CLICK, this.slotClick);
            if (!_arg3){
                this.tweenComplete(_local4);
                return;
            };
            switch (this.direction){
                case DIRECTION_HORI:
                    TweenLite.to(_local4, this.m_fadeOutTweenTime, {
                        y:(_local4.y + 80),
                        alpha:0,
                        onComplete:this.tweenComplete,
                        onCompleteParams:[_local4]
                    });
                    break;
                case DIRECTION_VERT:
                    TweenLite.to(_local4, this.m_fadeOutTweenTime, {
                        x:(_local4.x + 80),
                        alpha:0,
                        onComplete:this.tweenComplete,
                        onCompleteParams:[_local4]
                    });
                    break;
            };
        }
        public function shift(_arg1:ContainerSlot):void{
            var _local2:int;
            var _local3:int;
            var _local4:Boolean;
            switch (this.direction){
                case DIRECTION_HORI:
                    _local2 = _arg1.width;
                    break;
                case DIRECTION_VERT:
                    _local2 = this.CSLOT_HEIGHT;
                    break;
            };
            _arg1.position++;
            _local3 = ((-((_local2 + this.m_slotDistance)) / 2) + (_arg1.position * this.m_slotDistance));
            switch (this.direction){
                case DIRECTION_HORI:
                    TweenLite.to(_arg1, this.m_shiftTweenTime, {
                        y:0,
                        x:_local3,
                        alpha:1
                    });
                    break;
                case DIRECTION_VERT:
                    _local4 = RuntimeVariableManager.getBoolean("CLIENT_TWEEN_QUESTS", true);
                    if (_local4 == true){
                        TweenLite.to(_arg1, this.m_shiftTweenTime, {
                            x:0,
                            y:_local3,
                            alpha:1
                        });
                    } else {
                        _arg1.x = 0;
                        _arg1.y = _local3;
                        _arg1.alpha = 1;
                    };
                    break;
            };
        }
        public function unshift(_arg1:ContainerSlot):void{
            var _local2:int;
            switch (this.direction){
                case DIRECTION_HORI:
                    _local2 = _arg1.width;
                    break;
                case DIRECTION_VERT:
                    _local2 = this.CSLOT_HEIGHT;
                    break;
            };
            _arg1.position--;
            var _local3:int = ((-((_local2 + this.m_slotDistance)) / 2) + (_arg1.position * this.m_slotDistance));
            switch (this.direction){
                case DIRECTION_HORI:
                    TweenLite.to(_arg1, this.m_shiftTweenTime, {
                        y:0,
                        x:_local3
                    });
                    break;
                case DIRECTION_VERT:
                    TweenLite.to(_arg1, this.m_shiftTweenTime, {
                        x:0,
                        y:_local3
                    });
                    break;
            };
        }
        public function removeSlot(_arg1:ContainerSlot):void{
            var _local2:ContainerSlot;
            var _local3:uint;
            var _local4:ContainerSlot;
            for each (_local2 in this.resourcePool) {
                if (_local2.position == _arg1.position){
                    _arg1 = _local2;
                };
            };
            _local3 = _arg1.position;
            for each (_local4 in this.resourcePool) {
                if (_local4.position >= _local3){
                    this.unshift(_local4);
                };
            };
            _arg1.removeEventListener(MouseEvent.CLICK, this.slotClick);
            switch (this.direction){
                case DIRECTION_HORI:
                    TweenLite.to(_arg1, this.m_fadeOutTweenTime, {
                        y:(_arg1.y + 80),
                        alpha:0,
                        onComplete:this.tweenComplete,
                        onCompleteParams:[_arg1]
                    });
                    break;
                case DIRECTION_VERT:
                    TweenLite.to(_arg1, this.m_fadeOutTweenTime, {
                        x:(_arg1.x + 80),
                        alpha:0,
                        onComplete:this.tweenComplete,
                        onCompleteParams:[_arg1]
                    });
                    break;
            };
        }
        public function debugMode():void{
            var _local3:int;
            var _local1:MovieClip = new MovieClip();
            _local1.graphics.beginFill(0xFF0000, 1);
            _local1.graphics.drawRect(0, 0, 15, 15);
            _local1.graphics.endFill();
            _local1.addEventListener(MouseEvent.CLICK, this.debugPush);
            addChild(_local1);
            var _local2 = -1;
            while (_local2 < this.num_slots) {
                _local1 = new MovieClip();
                _local1.graphics.beginFill(0xFF0000, 1);
                _local1.graphics.drawCircle(0, 0, 2);
                _local1.graphics.endFill();
                _local3 = ((_local2 * (this.totalDistance / this.num_slots)) + (this.m_slotDistance / 2));
                switch (this.direction){
                    case DIRECTION_HORI:
                        _local1.x = _local3;
                        _local1.y = (this.constantDistance / 2);
                        break;
                    case DIRECTION_VERT:
                        _local1.y = _local3;
                        _local1.x = (this.constantDistance / 2);
                        break;
                };
                addChild(_local1);
                _local2++;
            };
        }
        public function debugPush(_arg1:MouseEvent):void{
            var _local2:MovieClip;
            if (this.m_containerLock == false){
                _local2 = new MovieClip();
                _local2.graphics.beginFill(0xFF00, 0.6);
                _local2.graphics.drawRect(0, 0, 40, 40);
                _local2.graphics.endFill();
                this.push(_local2);
            };
        }
        public function debugOutput():void{
            var _local3:ContainerSlot;
            var _local1:uint;
            var _local2:uint;
            for each (_local3 in this.resourcePool) {
                if (_local3.available){
                    _local1++;
                } else {
                    _local2++;
                };
            };
        }
        private function generateMask():Sprite{
            var _local1:Sprite = new Sprite();
            _local1.graphics.beginFill(0xFF0000, 0.5);
            _local1.graphics.drawRect(-10, 0, this.m_box_width, this.m_box_height);
            _local1.graphics.endFill();
            return (_local1);
        }
        private function tweenComplete(_arg1:ContainerSlot):void{
            _arg1.removeEventListener(MouseEvent.CLICK, this.slotClick);
            _arg1.reset();
            this.m_containerLock = false;
        }
        public function get tweenTime():Number{
            return ((this.m_shiftTweenTime + this.m_fadeOutTweenTime));
        }

    }
}//package Modules.quest.Display 
